#include "ElasticInOutLayer.h"
#include "CocosGUI.h"
USING_NS_CC;

bool ElasticInOutLayer::init()
{
	auto viewSize = Director::sharedDirector()->getVisibleSize();
	
	spr1 = Sprite::create("sh1.png");
	spr1->setPosition(Vec2(0 - 130,viewSize.height/2));
	spr2 = Sprite::create("sh1.png");
	spr2->setPosition(Vec2(0 - 130,viewSize.height/2 - 100));
	spr3 = Sprite::create("sh1.png");
	spr3->setPosition(Vec2(0 - 130,viewSize.height/2 - 190));
	
	this->addChild(spr1,300);
	this->addChild(spr2,200);
	this->addChild(spr3,100);

	// add a "close" icon to exit the progress. it's an autorelease object
	auto fadIn = MenuItemImage::create(
		"CloseNormal.png",
		"CloseSelected.png",
		CC_CALLBACK_1(ElasticInOutLayer::fadeIn, this));

	// add a "close" icon to exit the progress. it's an autorelease object
	auto fadOut = MenuItemImage::create(
		"CloseNormal.png",
		"CloseSelected.png",
		CC_CALLBACK_1(ElasticInOutLayer::fadeOut, this));

	// create menu, it's an autorelease object
	auto menu = Menu::create(fadIn,fadOut,NULL);
	menu->alignItemsHorizontally();
	menu->setPosition(Vec2(700,50));
	menu->setAnchorPoint( Vec2::ANCHOR_MIDDLE);
	this->addChild(menu, 1);

	/*move = MoveBy::create(3,Vec2(viewSize.width - 50,0));
	move_back = move->reverse();*/

	
	return true;
}

void ElasticInOutLayer::fadeIn(Ref* pSender)
{
    auto delay = DelayTime::create(0.1f);
	auto viewSize = Director::sharedDirector()->getVisibleSize();

	auto move = MoveBy::create(1,Vec2(viewSize.width/2 + 260,0));
	auto move_back = move->reverse();

	auto sp1_elastic_in = EaseElasticIn::create(move->clone(),0.3f);
	auto sp2_elastic_in = EaseElasticIn::create(move->clone(),0.5f);
	auto sp3_elastic_in = EaseElasticIn::create(move->clone(),0.7f);

	auto sequ1 = Sequence::create(sp1_elastic_in,delay,NULL);
	auto sequ2 = Sequence::create(sp2_elastic_in,delay->clone(),NULL);
	auto sequ3 = Sequence::create(sp3_elastic_in,delay->clone(),NULL);
	
	spr1->runAction(Repeat::create(sequ1,1));
	spr2->runAction(Repeat::create(sequ2,1));
	spr3->runAction(Repeat::create(sequ3,1));
}

void ElasticInOutLayer::fadeOut(Ref* pSender)
{
	delayTime = DelayTime::create(0.1f);
	auto viewSize = Director::sharedDirector()->getVisibleSize();
	auto move = MoveBy::create(1,Vec2(viewSize.width/2 + 260,0));
	auto move_back = move->reverse();

	auto sp1_elastic_out = EaseElasticIn::create(move_back->clone(),0.3f);
	auto sp2_elastic_out = EaseElasticIn::create(move_back->clone(),0.5f);
	auto sp3_elastic_out = EaseElasticIn::create(move_back->clone(),0.7f);

	auto sequ1 = Sequence::create(sp1_elastic_out,delayTime->clone(),NULL);
	auto sequ2 = Sequence::create(sp2_elastic_out,delayTime->clone(),NULL);
	auto sequ3 = Sequence::create(sp3_elastic_out,delayTime->clone(),NULL);

	spr1->runAction(Repeat::create(sequ1,1));
	spr2->runAction(Repeat::create(sequ2,1));
	spr3->runAction(Repeat::create(sequ3,1));
}